package com.lrkj.waitress.service.impl;

import java.util.List;

import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.waitress.domain.PathDetail;
import com.lrkj.waitress.domain.Place;
import com.lrkj.waitress.service.IPathDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.waitress.mapper.PathMapper;
import com.lrkj.waitress.domain.Path;
import com.lrkj.waitress.service.IPathService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.unit.DataUnit;

/**
 * 路线管理Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-03-22
 */
@Service
public class PathServiceImpl implements IPathService
{
    @Autowired
    private PathMapper pathMapper;


    @Autowired
    private ICommunityService communityService;

    @Autowired
    private IPathDetailService pathDetailService;

    /**
     * 查询路线管理
     *
     * @param id 路线管理ID
     * @return 路线管理
     */
    @Override
    public Path selectPathById(String id)
    {
        return pathMapper.selectPathById(id);
    }

    /**
     * 查询路线管理列表
     *
     * @param path 路线管理
     * @return 路线管理
     */
    @Override
    public List<Path> selectPathList(Path path)
    {
        return pathMapper.selectPathList(path);
    }

    /**
     * 新增路线管理
     *
     * @param path 路线管理
     * @return 结果
     */
    @Override
    public int insertPath(Path path)
    {
        path.setId(IdUtils.simpleUUID());
        path.setCreateBy(SecurityUtils.getUsername());
        path.setCreateTime(DateUtils.getNowDate());
        this.setInfo(path);
        this.createDetail(path);
        return pathMapper.insertPath(path);
    }

    /**
     * 修改路线管理
     *
     * @param path 路线管理
     * @return 结果
     */
    @Override
    public int updatePath(Path path)
    {
        path.setUpdateBy(SecurityUtils.getUsername());
        path.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(path);
        this.createDetail(path);
        return pathMapper.updatePath(path);
    }

    /**
     * 批量删除路线管理
     *
     * @param ids 需要删除的路线管理ID
     * @return 结果
     */
    @Override
    public int deletePathByIds(String[] ids)
    {
        return pathMapper.deletePathByIds(ids);
    }

    /**
     * 删除路线管理信息
     *
     * @param id 路线管理ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePathById(String id)
    {
        //删除子表数据
        pathDetailService.deletePathId(id);
        return pathMapper.deletePathById(id);
    }
    /**
     * 批量新增路线管理信息
     *
     * @param paths 路线管理对象
     */
    public int batchAddData(List<Path> paths)
    {
           return pathMapper.batchAddData(paths);
    }



    private void setInfo(Path path) {
        Community community = communityService.selectCommunityById(path.getCommunityId());
        path.setDeptId(community.getDeptId());
        path.setAncestors(community.getAncestors() + "," + community.getId());
    }


    /**
     * 添加子表数据
     * @param path
     */
    private void createDetail(Path path) {
        if (StringUtils.isNotEmpty(path.getId())){
            //ID不为空 根据路线id删除明细数据
                pathDetailService.deletePathId(path.getId());
        }
        if (path.getPathDetailList()==null){
            throw  new BaseException("请添加地点!");
        }
        path.getPathDetailList().stream().forEach(x ->{
            if (x.getPlaceId()==null){
                throw  new BaseException("请选择地点");
            }
            x.setId(IdUtils.simpleUUID());
            x.setPathId(path.getId());
            x.setCreateBy(SecurityUtils.getUsername());
            x.setCreateTime(DateUtils.getNowDate());
            x.setCommunityId(path.getCommunityId());
            x.setCrulserId(path.getCrulserId());
            x.setCrulserBegin(path.getCrulserBegin());
            x.setCrulserEnd(path.getCrulserEnd());
            x.setCrulserType(path.getCrulserType());
            pathDetailService.insertPathDetail(x);
        });

    }

}
